//
//  LoginViewController.swift
//  chenChat
//
//  Created by chen on 16/2/19.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController,UIScrollViewDelegate,UITextFieldDelegate {

    @IBOutlet var scrollview: UIScrollView!
    @IBOutlet var imageView: UIImageView!
    @IBOutlet var usernameLabel: UILabel!
    @IBOutlet var pwdLabel: UILabel!
    
    @IBOutlet var userNameView: UIView!
    @IBOutlet var userNameTF: UITextField!
    @IBOutlet var pwdView: UIView!
    @IBOutlet var pwdTF: UITextField!
    @IBOutlet var onButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollview.delegate = self
        userNameTF.delegate = self
        pwdTF.delegate = self
        let screenRect: CGRect = UIScreen.mainScreen().bounds
//      self.scrollview.contentSize = CGSizeMake(screenRect.width, screenRect.height + 5)
        self.pwdTF.secureTextEntry = true
        

        
    }

    @IBAction func loginButton(sender: AnyObject) {
        if (self.userNameTF.text!.isEmpty || self.pwdTF.text!.isEmpty){
            return
        }
        let trimUserName = self.userNameTF.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        let trimPwd = self.pwdTF.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        NSUserDefaults.standardUserDefaults().setValue(trimUserName, forKey: "username")
        NSUserDefaults.standardUserDefaults().setValue(trimPwd, forKey: "password")
        NSUserDefaults.standardUserDefaults().synchronize()
        //调用了AppDelegate的方法login
        (UIApplication.sharedApplication().delegate as! AppDelegate).login()
        //将当前的view视图给移除
        self.dismissViewControllerAnimated(true) { () -> Void in
            
        }
        
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offset = self.scrollview.contentOffset.y / 2
        let transform = CGAffineTransformMakeTranslation(0, offset)
        self.imageView.transform = transform
        self.userNameTF.resignFirstResponder()
        self.pwdTF.resignFirstResponder()
    }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField == self.userNameTF{
            self.userNameTF.resignFirstResponder()
            self.pwdTF.becomeFirstResponder()
        }else{
            self.pwdTF.resignFirstResponder()
            self.loginButton(self.onButton)
        }
        return false
    }
}
